package com.hxd.buybooksystem.ui.activity

import android.support.v7.widget.LinearLayoutManager
import cn.bmob.v3.BmobQuery
import cn.bmob.v3.exception.BmobException
import cn.bmob.v3.listener.FindListener
import com.hxd.buybooksystem.R
import com.hxd.buybooksystem.adapter.HistoryOrderAdapter
import com.hxd.buybooksystem.base.common.MyActivity
import com.hxd.buybooksystem.bean.Order
import com.hxd.buybooksystem.uitls.L
import kotlinx.android.synthetic.main.activity_history_order.*

class HistoryOrderActivity : MyActivity() {

    private val historyOrders by lazy { ArrayList<Order>() }

    override fun getLayoutId(): Int {
        return R.layout.activity_history_order
    }

    override fun getTitleId(): Int {
        return R.id.tb_title_historyOrder
    }

    override fun initView() {

    }

    override fun initData() {
        loadData()
    }

    private fun loadData(){
        val query = BmobQuery<Order>()
        waitDialog
        query.findObjects(object : FindListener<Order>() {
            override fun done(litis: MutableList<Order>, e: BmobException?) {
                waitDialog.dismiss()
                if (e == null) {
                    litis.forEach {
                        if (it.isPay){
                            historyOrders.add(it)
                        }
                    }
                    setRecycle()
                    successDialog
                } else {
                    errorDialog
                    L.i("订单数据查询失败")
                }
            }
        })
    }

    private fun setRecycle(){
        //设置适配器
        val historyAdapter = HistoryOrderAdapter(historyOrders)
        recycle_historyOrder.adapter = historyAdapter
        val linearManage = LinearLayoutManager(this)
        linearManage.orientation = LinearLayoutManager.VERTICAL
        recycle_historyOrder.layoutManager = linearManage
    }

}
